﻿using System;
using System.Collections.Generic;
using System.ComponentModel.DataAnnotations;
using System.ComponentModel.DataAnnotations.Schema;
using System.Data.Entity;
using System.Linq;
using System.Web;

namespace CalendarApp.Models
{
    public class Entry
    {
        public int ID { get; set; }
        public string Title { get; set; }
        public string Description { get; set; }
        public DateTime? StartDate { get; set; }
        public DateTime? EndDate { get; set; }
    }

    public class PersonalEntry
    {
        public Entry EntryBase { get; set; }
        public Category Category { get; set; }
        
        public PersonalEntry(Entry e)
        {
            EntryBase = e;
        }

        public void AssignToCategory(Category c)
        {
            if (Category != null)
            {
                Category.RemoveEntry(this);
                c.AddEntry(this);
            }
            Category = c;
        }
    }

    public class Category
    {
        public int ID { get; set; }
        public string Name { get; set; }
        public string Color { get; set; }
        public ICollection<PersonalEntry> Entries { get; set; }
        public void AddEntry(PersonalEntry pe)
        {
            Entries.Add(pe);
        }
        public void RemoveEntry(PersonalEntry pe)
        {
            if (Entries.Contains(pe)) Entries.Remove(pe);
        }
    }

    public class CategoryFilter 
    {
        public int ID { get; set; }
        public Category Category { get; set; }
        public bool Visible { get; set; }
    }

    public class EntryFilter 
    {
        public int ID { get; set; }
        public PersonalEntry Entry { get; set; }
        public bool Visible { get; set; }
    }

    public abstract class Role
    {
        public Calendar Calendar { get; set;}
        
        public void showCalendar()
        {
            Calendar.show();
        }

        public abstract bool EditCalendar();
        public abstract bool DeleteCalendar();
        public abstract bool manageRoles();
    }

    public class OwnerRole : Role
    {
        public override bool EditCalendar()
        {
            return true;
        }

        public override bool DeleteCalendar()
        {
            return true;
        }

        public override bool manageRoles()
        {
            return true;
        }
    }

    public class EditorRole : Role
    {
        public override bool EditCalendar()
        {
            return true;
        }

        public override bool DeleteCalendar()
        {
            return false;
        }

        public override bool manageRoles()
        {
            return false;
        }
    }

    public class MemberRole : Role
    {
        public override bool EditCalendar()
        {
            return false;
        }

        public override bool DeleteCalendar()
        {
            return false;
        }

        public override bool manageRoles()
        {
            return false;
        }
    }

    public class Calendar
    {
        public int ID { get; set; }
        public string Name { get; set; }
        public ICollection<CategoryFilter> CategoryFilters { get; set; }
        public ICollection<EntryFilter> EntryFilters { get; set; }
        public ICollection<Role> Roles { get; set; } 
        
        public Calendar()
        {
            OwnerRole owner = new OwnerRole();
            Roles.Add(owner);
        }

        public void show() { }
        
        public void AddCategory(Category c)
        {
            foreach (CategoryFilter cf in CategoryFilters)
            {
                if (cf.Category == c) return;
            }

            CategoryFilter newCF = new CategoryFilter();
            newCF.Category = c;
            newCF.Visible = true;
        }
        public void RemoveCategory(Category c)
        {
            foreach (CategoryFilter cf in CategoryFilters)
            {
                if (cf.Category == c) CategoryFilters.Remove(cf);
            }
        }

        public void AddEntry(PersonalEntry e)
        {
            foreach (EntryFilter ef in EntryFilters)
            {
                if (ef.Entry == e) return;
            }

            EntryFilter newEF = new EntryFilter();
            newEF.Entry = e;
            newEF.Visible = true;
        }

        public void RemoveEntry(PersonalEntry e)
        {
            foreach (EntryFilter ef in EntryFilters)
            {
                if (ef.Entry == e) EntryFilters.Remove(ef);
            }
        }

        /*
         * public Role LookupMember(string name)
         * {
         *      // nev keresese, eredmeny visszakuldese
         * }
         */

        public void AddMember(string name)
        {
            /* if (thisSession.currentRole.ManageRoles())
             * {
             *      if (LookupMember(name) == null){
             *      // ha nem talalt, uj MemberRole letrehozasa
             *      // uj role felvetele a kollekcioba
             *      }      
             * } 
             */
        }

        public void AddEditor(string name)
        {
            /* if (thisSession.currentRole.ManageRoles())
             * {
             *      if (LookupMember(name) == null){
             *      // ha nem talalt, uj EditorRole letrehozasa
             *      // uj role felvetele a kollekcioba
             *      }
             * } 
             */
        }

        public void RemoveMember(string name)
        {
            /* if (thisSession.currentRole.ManageRoles())
             * {
             *      Role role = LookupMember(name);
             *      if (role != null){
             *          Roles.Remove(role)
             *      }
             * } 
             */
        }

        public void setMember(string name)
        {
            /* if (thisSession.currentRole.ManageRoles())
             * {
             *      Role role = LookupMember(name);
             *      if (role != null){
             *          Role newRole = new MemberRole();
             *          // role-ok csereje
             *      }
             * } 
             */
        }

        public void setEditor(string name)
        {
            /* if (thisSession.currentRole.ManageRoles())
             * {
             *      Role role = LookupMember(name);
             *      if (role != null){
             *          Role new Role = new EditorRole();
             *          // role-ok csereje
             *      }
             * } 
             */
        }
    }

    public class CalendarDBContext : DbContext
    {
        public DbSet<Entry> Entries { get; set; }
    }
}